//客户端快捷方式
Ext.define("ES.controller.ClientShortcutController",{
    extend:"Ext.app.Controller",
    models:["ClientShortcutModel","CatalogModel"],
    stores:["CatalogStore","ClientShortcutStore","ShortcutTypeStore","ShortcutStatusStore","LinkTargetStore"],
    views:["clientshortcut.ClientShortcutList","clientshortcut.ClientShortcutEdit"],
    init:function(){
        this.control({
            "clientShortcutTabList":{
                afterrender:function(cmp){
                    var typeStore = this.getShortcutTypeStoreStore();
                    var statusStore = this.getShortcutStatusStoreStore();
                    var targetStore = this.getLinkTargetStoreStore();
                    if(typeStore != null && statusStore != null && targetStore != null){
                        typeStore.loadPage(1,{
                            callback:function(){
                              cmp.setEnumTypeStore(typeStore);
                            }
                        });
                        statusStore.loadPage(1,{
                            callback:function(){
                               cmp.setEnumStatusStore(statusStore);
                            }
                        });
                        targetStore.loadPage(1,{
                            callback:function(records, operation, success){
                               cmp.setEnumTargetStore(targetStore);
                               if(success){
                                   var btn = cmp.down("toolbar button[action=btnQuery]");
                                   if(btn){
                                        var toolbar =  btn.ownerCt;
                                        var grid = toolbar.ownerCt;       
                                        var st = grid.getStore();
                                        st.loadPage(1);
                                   }
                               }
                            }
                        });
                    }
                },
                itemdblclick:this.onEdit
            },
            "clientShortcutTabList button[action=btnQuery]":{
                click:this.onSearch
            },
            "clientShortcutTabList button[action=btnAdd]":{
                click:this.onAdd
            },
            "clientShortcutTabList button[action=btnDelete]":{
                click:this.onDelete
            },
            "clientShortcutEdit button[action=btnSave]":{
                click:this.onSave
            },
            "clientShortcutEdit button[action=btnCancel]":{
                click:function(btn){
                     var win = btn.up("clientShortcutEdit");
                     if(win){
                         win.close();
                     }
                } 
            }
        });
    },
    onSearch:function(btn){
       var toolbar =  btn.ownerCt;
       var grid = toolbar.ownerCt;       
       var st = grid.getStore();
       st.loadPage(1,{
           params:{
               shortcutName:toolbar.getComponent(1).getValue()
           }
       });
   },
   onAdd:function(){
        var win = Ext.widget("clientShortcutEdit");
        if(win){
            win.show();
        }
  },
  onEdit:function(view, record, item,index,e,opt){
        var win = Ext.widget("clientShortcutEdit");
        if(win){
            win.show();
            var el = win.getEl(); 
            el.mask("正在加载数据，请稍后....");
            if(record){
                var frm = win.down("form");
                if(frm){
                    frm.loadRecord(record);
                }
            }
            el.unmask();
        }
   },
   onSave:function(btn){
        var win = btn.up("clientShortcutEdit");
        var form = btn.up("form").getForm();
        if(form.isValid()){
            var store = this.getClientShortcutStoreStore();
            var record = form.getRecord();
            var values = form.getValues();
            if(record){
                record.set(values);
            }else{
                record = this.getClientShortcutModelModel().create();
                record.set(values);
                store.insert(0,record);
            }
            store.sync();
            win.close();
        }
    },
    onDelete:function(btn){
        Ext.MessageBox.confirm("删除提示","确认要删除选中的数据吗？", function(cfm){
            if(cfm == "yes"){
                var grid = btn.ownerCt.ownerCt;
                var data = grid.getSelectionModel().getSelection();
                if(data.length == 0){
                    Ext.Msg.alert("提示","您最少要选择一条数据!");
                }else{
                    var el = grid.getEl()
                    el.mask("正在删除数据，请稍后...");
                    var st = grid.getStore();
                    Ext.Array.each(data, function(record){
                        st.remove(record);
                    })
                    st.sync();
                    el.unmask();
                }
            }
        });
    }
})